<?php
/**
 * 商品属性接口控制器
 */
namespace Admin\Controller;

use Think\Controller;
use Think\Log;

class GoodsAttrController extends ApiBaseController
{
    
	/* 新增商品多属性 */
	public function addGoodsAttr(){		
		
		//先验证接口是否为合法调用
		$request_token= I('request.token','','trim');

		//得到系统token
		$local_token=C('token');
		//如果token不一致，则返回消息。
		if($request_token !=$local_token ){
			$result = array('resultCode'=>0,'resultMsg'=>'token 校验不一致，无权进行操作');
			echo json_encode($result);
			exit;
		}
		
		$attrsData = I('request.attrsData','','trim');
		$data_type = I('request.dt','','trim');
        
		$attr_data = $attrsData;//json_decode($attrsData,true);
		
		$fail_sync_ids = array();//同步失败ids
		$succ_sync_ids = array();
		//检测字符串编码
        // 		$str = 'xx宿舍水电费第三方';
        // 		$encode = mb_detect_encoding($str, array('ASCII','GB2312','GBK','UTF-8'));
        // 		\Think\Log::write('测试日志信息，这是警告级别，并且实时写入,'.$encode,'WARN');
        
		if( empty($attr_data) ){
			$result = array('resultCode'=>0,'resultMsg'=>'未检测到需要同步的多属性数据/或已全部同步完成');
			echo json_encode($result);
			exit;
		}

        

        switch ($data_type) {
            //编辑 goods_attribute_category
            case '1':
                $this->change_goods_attribute_category($attr_data);
                break;
            //编辑 goods_attribute_group
            case '2':
                $this->change_goods_attribute_group($attr_data);
                break;
            //编辑 goods_attribute_value
            case '3':
                $this->change_goods_attribute_value($attr_data);
                break;
            default:
                
                break;
        }

        

		$result = array('resultCode'=>1,'resultMsg'=>'同步动作完成');
		echo json_encode($result);
		exit;
	}
	
	/* 编辑商品多属性 */
	public function editGoodsAttr(){
		
		
	}
	
	/* 删除商品多属性 */
	public function delGoodsAttr(){
		
	}


    //改变多属性大类的对象集合
	public function change_goods_attribute_category($attr_data){

		//数据库 db
		$goods_attribute_category = M('goods_attribute_category');
        

	    //循环数据操作
		foreach ($attr_data as $cat_data){
			
			$catData = array();
			$cat_record = $goods_attribute_category->where(array('id'=>$cat_data['id']))->find();
			$catData['id'] = $cat_data['id'];
			$catData['name'] = $cat_data['name'];
			$catData['sort'] = $cat_data['sort'];
			$catData['remark'] = $cat_data['remark'];			
			
			if( empty($cat_record) ){//如果记录不存在就添加
				$res = $goods_attribute_category->add($catData);
			}else{
				//存在就修改
				unset($catData['id']);
				$res = $goods_attribute_category->where(array('id'=>$cat_data['id']))->save($catData);
			}

		}
	}

    //改变多属性分组的对象集合
	public function change_goods_attribute_group($attr_data){

        $goods_attribute_group = M('goods_attribute_group');
		

	    //循环数据操作
		foreach ( $attr_data as $group_data ){
            
            $groupData = array();
            $group_record = $goods_attribute_group->where(array('id'=>$group_data['id']))->find();
            $groupData['id'] = $group_data['id'];
            $groupData['name'] = $group_data['name'];
            $groupData['sort'] = $group_data['sort'];
            $groupData['is_image'] = $group_data['is_image'];
            $groupData['remark'] = $group_data['remark'];
            $groupData['cat_id'] = $group_data['cat_id'];
            $groupData['createtime'] = $group_data['createtime'];
            $groupData['is_delete'] = $group_data['is_delete'];				
            
            if( empty($group_record) ){//如果记录不存在就添加
                $res = $goods_attribute_group->add($groupData);
            }else{
                unset($groupData['id']);
                $res = $goods_attribute_group->where(array('id'=>$group_data['id']))->save($groupData);
            }

		}
	}


	//改变多属性分组值的对象集合
	public function change_goods_attribute_value($attr_data){

		$goods_attribute_value = M('goods_attribute_value');

        foreach ( $attr_data as $val_data ){
            
			$valData = array();
			$val_record = $goods_attribute_value->where(array('id'=>$val_data['id']))->find();
			$valData['id'] = $val_data['id'];
			$valData['attribute_group_id'] = $val_data['attribute_group_id'];
			$valData['sort'] = $val_data['sort'];
			$valData['is_delete'] = $val_data['is_delete'];
			$valData['value'] = $val_data['value'];

			//如果记录不存在就添加
			if( empty($val_record) ){
				$res = $goods_attribute_value->add($valData);
			}else{
				unset($valData['id']);
				$res = $goods_attribute_value->where(array('id'=>$val_data['id']))->save($valData);						
			}
		}
	}
	
	
	
}
